package jcommon.chap1_j2seplus.date;

import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang.time.FastDateFormat;

public class Round_TruncateDate {
	
	private static FastDateFormat fdf = DateFormatUtils.ISO_DATETIME_FORMAT;

	public static void main(String[] args) {
		Date now = new Date( );
		
		// 四舍五入
		Date nearestHour = DateUtils.round( now, Calendar.HOUR );
		printDate(nearestHour);
		Date nearestDay = DateUtils.round( now, Calendar.DAY_OF_MONTH );
		printDate(nearestDay);
		Date nearestYear = DateUtils.round( now, Calendar.YEAR );
		printDate(nearestYear);
		
		// floor
		Date truncatedMonth = DateUtils.truncate( now, Calendar.MONTH );
		printDate(truncatedMonth);
		Date truncatedHour = DateUtils.truncate( now, Calendar.HOUR ); 
		printDate(truncatedHour);
	}
	
	private static void printDate(Date date) {
		System.out.println(fdf.format(date));
	}
}
